<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class ZlFeedbackTag extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_feedback_tag';

    protected $cacheModel = 'feedback.tags';

    /**
     * 获取反馈标签信息
     * @param int $type     标签类型
     *      1=>用户反馈标签
     *      2=>反馈内容所属标签
     *      3=>年运反馈标签  * 年运运势带给您的感觉是？
     *      4=>年运反馈标签  * 年运曲线带给您的感觉是？
     *      5=>年运反馈标签  * 阶段运势带给您的感觉是？
     * @param array $field
     * @return mixed
     * @throws \Exception
     */
    public function getTags(int $type, array $field = []): array
    {
        $data = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $type)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($type)
            {
                if ($isCache)
                { // 开启了缓存
                    return $this->getInfoByIdsInData($ids, [$type], $sortType);
                } else
                { // 没开启缓存
                    return $this->getPaginateInData([$type], $sortType);
                }
            }, 'sort', $field, 0);

        return $data['data'] ?? [];
    }

    /**
     * 获取所有标签的ids
     * @param int $type     标签类型 1=>用户反馈标签 2=>反馈内容所属标签
     * @return array|mixed
     * @throws \Exception
     */
    public function getTagIds(int $type)
    {
        $tags = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $type)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPageDataIds(function ($ids = [], $isCache = true, $sortType = 'desc') use ($type)
            {
                if ($isCache)
                { // 开启了缓存
                    return $this->getInfoByIdsInData($ids, [$type], $sortType);
                } else
                { // 没开启缓存
                    return $this->getPaginateInData([$type], $sortType);
                }
            }, 'sort', 0);

        return isset($tags['data']) ? array_keys($tags['data']) : [];
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->whereIn('id', $ids);
            } else
            {
                $this->where(['type' => $type[0], 'status' => 1]);
            }
            $data = $this->order("sort {$sortType}")->column('name,keyword,sort', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 未开启缓存从数据库获取分页数据
     * @param array $type 分页数据状态
     * @param string $sortType 排序方式
     * @return array
     */
    public function getPaginateInData($type, $sortType)
    {
        try
        {
            $data = $this->where(['type' => $type[0], 'status' => 1])
                ->field('id,name,keyword,sort')
                ->order('sort ' . $sortType)
                ->paginate()->toArray();
            return $data;
        }
        catch (\Exception $e)
        {
            return [];
        }
    }
}
